﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using BrightIdeasSoftware;
using MovieBrowser.Model;

namespace MovieBrowser.Util
{
    public static class ComponentUtility
    {

        public static Color[] HardnessBackground = new[]{
                                                               Color.White, //0
                                                               Color.FromArgb(123, 82, 161),
                                                               Color.FromArgb(56, 82, 166),
                                                               Color.FromArgb(77, 133, 132),
                                                               Color.FromArgb(105, 189, 68),
                                                               Color.FromArgb(180, 211, 55),
                                                               Color.FromArgb(246, 235, 19),
                                                               Color.FromArgb(244, 189, 28),
                                                               Color.FromArgb(247, 143, 30),
                                                               Color.FromArgb(242, 99, 33),
                                                               Color.FromArgb(237, 32, 37),
                                                               };

        public static Color[] HardnessForeground = new[]
                                                           {
                                                               Color.Black, //0
                                                               Color.White, 
                                                               Color.White, 
                                                               Color.White, 
                                                               Color.White,
                                                               Color.Navy, 
                                                               Color.Black, 
                                                               Color.Black,
                                                               Color.White, 
                                                               Color.White,
                                                               Color.White,
    };

        public static void TimedFilter(ObjectListView olv, string txt)
        {
            TextMatchFilter filter = null;
            if (!String.IsNullOrEmpty(txt))
                filter = new TextMatchFilter(olv, txt);

            // Setup a default renderer to draw the filter matches
            olv.DefaultRenderer = filter == null ? null : new HighlightTextRenderer(filter);

            // Some lists have renderers already installed
            var highlightingRenderer = olv.GetColumn(0).Renderer as HighlightTextRenderer;
            if (highlightingRenderer != null)
                highlightingRenderer.Filter = filter;

            olv.ModelFilter = filter;
        }

        public static IList<Movie> ToIList(this ListView.SelectedListViewItemCollection @this)
        {
            return (from object item in @this select ((OLVListItem)item).RowObject as Movie).ToList();
        }

        public static void FormatRow(object sender, FormatRowEventArgs e)
        {
            var movie = (Movie)e.Model;
            e.Item.BackColor = movie.Seen ? Color.PaleTurquoise : Color.White;
            e.Item.Font = movie.Watchlist ? new Font(e.Item.Font, e.Item.Font.Style | FontStyle.Bold) : e.Item.Font;
            e.Item.ForeColor = movie.Watchlist ? Color.Blue : e.Item.ForeColor;
            e.Item.ForeColor = movie.Have ? Color.MediumVioletRed : e.Item.ForeColor;

        }
    }
}
